<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
$uniacid=$_SESSION['uniacid'];
$mid=$_SESSION['uid'];
if($_RQ['op']=='main'){
	$basic=$this->plugin_setting('basic');
	if($basic['sysmid']){
		foreach($basic['sysmid'] as &$v){
			if($v['mid'] == $_SESSION['uid']){
				$saler =true;
			}
		}
	}
	$verifycode=$_RQ['verifycode'];
	$id=$_RQ['id'];
	if (empty($verifycode) && !$id) {
		exi('请输入核销码!','error','jy_quan/verify.index/');
	}
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($verifycode){
		$params['verifycode']=$verifycode;
	}else{
		$params['id']=$id;
	}
	$order=pdo_get('jy_quan_item_jieli',$params);
	if (empty($order)) {
		exi('接龙信息不存在!','error','jy_quan/verify.index/');
	}
	if (!$saler){
		$pidmid=pdo_getcolumn('jy_quan_item',array('id'=>$order['pid']),'mid');
		if($pidmid!=$_SESSION['uid']){
			exi('该活动不是你发起的，您也不属于管理员!','error','jy_quan/verify.index/');	
		}
	}
	//用户信息
	$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['mid']));
	//项目信息
	$item=pdo_get('jy_quan_item',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['pid']));	
	//自定义字段数据
	$order['groupsendsettitle']=$item['groupsendset']==1?'快递发货':$item['groupsendset']==0?'无需物流':'自提点提货';
	$formdata=pdo_get('jy_quan_item_formdata',array('mid'=>$order['mid'],'pid'=>$order['pid'],'uniacid'=>$_SESSION['uniacid']));
	$order['data']=json_decode($formdata['data'],true);
	foreach($order['data'] as &$dv){
		if($dv['type']==5 && $dv['data']){
			$dv['data'] = explode(";", $dv['data']);
			foreach($dv['data'] as $k=>&$i){
				if(empty($dv['data'][$k])){
					unset($dv['data'][$k]);
				}
			}
		}
	}
	$order['diydata']=json_decode($formdata['diydata'],true);
	foreach($order['diydata'] as &$v){
		if($v['type']==5 && $v['data']){
			$v['data'] = explode(";", $v['data']);
			foreach($v['data'] as $k=>&$i){
				if(empty($v['data'][$k])){
					unset($v['data'][$k]);
				}
			}
		}
	}
	$order['fileds']=json_decode($formdata['fileds'],true);
	foreach($order['fileds'] as &$fv){
		foreach($fv as $fk=>&$f){
			if($f['type']==5 && $fv['data']){
				$f['data'] = explode(";", $f['data']);
				foreach($f['data'] as $k=>&$i){
					if(empty($f['data'][$k])){
						unset($f['data'][$k]);
					}
				}
			}
			if($f['type']==18){
					$fv['total'] = $post['type']!=1?1:$fv['total'];
					if($fv['options']){
						$post['fee'] +=$fv['options'][$fv['optionid']]['price']*$fv['total'];
						$f['data']=$fv['options'][$fv['optionid']]['price'];
					}else{
						$post['fee'] +=$f['data']*$fv['total'];
					}
			}
		}
		if($fv['options']){
			$fv[]=array('title'=>'所选规格','type'=>'option','data'=>$fv['options'][$fv['optionid']]['title']);
		}
	}
	$item['typestatus']=$this->jili_status[$item['status']];
	$order['typestatus']=$this->jili_status[$order['status']];
	if($order['status']>=2){
				$order['express']=pdo_get('jy_quan_order_express',array('order_id'=>$order['id'],'uniacid'=>$_SESSION['uniacid']));
				$order['express']['express_name']=$return['express'][$order['express']['express_id']-1]['name'];
	}
	$order['typetitle']=$this->itemtypes()[$order['type']];
	$order['createtime']=date('Y-m-d H:i:s', $order['createtime']);
	$order['sendtime']=date('Y-m-d H:i:s', $order['sendtime']);
	$order['paytime']=date('Y-m-d H:i:s', $order['paytime']);
	$return['order']=$order;
	$return['member']=$member;
	$return['item']=$item;
	$return['post']=array(
		'orderid'=>$order['id'],
		'verify_code'=>$verifycode,
	);
	set_html('title','核销');
    exi($return);
}

if($_RQ['op']=='post'){
	$post=get_postdata();
	$orderid=intval($post['orderid']);
	$this->model('model')->order_verify($orderid,$_SESSION['uid']);
	exi('核销成功！','','jy_quan/verify.index/');
	
}